package com.ruoyi.web.controller.procon;

import java.math.BigDecimal;
import java.util.List;

import com.ruoyi.system.domain.ProCreateBuilding;
import com.ruoyi.system.domain.ProFileBlueprint;
import com.ruoyi.system.domain.Salary;
import com.ruoyi.system.service.IProCreateBuildingService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.domain.ProjectContract;
import com.ruoyi.service.IProjectContractService;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

import javax.annotation.Resource;

/**
 * 项目合同Controller
 *
 * @author ruoyi
 * @date 2020-06-22
 */
@Controller
@RequestMapping("/ruoyi/contract")
public class ProjectContractController extends BaseController {
    private String prefix = "ruoyi/contract";

    @Autowired
    private IProjectContractService projectContractService;

    @RequiresPermissions("ruoyi:contract:view")
    @GetMapping()
    public String contract() {
        return prefix + "/contract";
    }


    /**
     * 详情页面传递参数
     * @param bId
     * @param map
     * @return
     */
    @RequiresPermissions("ruoyi:contracts:view")
    @GetMapping("/contracts/{bId}")
    public String logs(@PathVariable("bId") Long bId, ModelMap map) {
        ProjectContract projectContract = projectContractService.selectProjectContractById(bId);
        map.put("projectContract", projectContract);
        return prefix + "/contracts";
    }

    /**
     * 查询项目合同列表
     */
    @RequiresPermissions("ruoyi:contract:list")
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(ProjectContract projectContract) {
        startPage();
        List<ProjectContract> list = projectContractService.selectProjectContractList(projectContract);
        return getDataTable(list);
    }

    /**
     * 查询项目合同列表
     */
    @RequiresPermissions("ruoyi:contract:list")
    @PostMapping("/lists")
    @ResponseBody
    public TableDataInfo lists(Long idss) {
        startPage();
        List<ProjectContract> list = projectContractService.selectProjectContractlistss(idss);
        return getDataTable(list);
    }


    /**
     * 查询对应项目下的对应合同
     */
    @RequiresPermissions("ruoyi:contract:listss")
    @PostMapping("/listss")
    @ResponseBody
    public TableDataInfo listss(Long sId) {
        startPage();
        List<ProjectContract> list = projectContractService.selectProjectContractlistss(sId);

        return getDataTable(list);
    }

    /**
     * 导出项目合同列表
     */
    @RequiresPermissions("ruoyi:contract:export")
    @Log(title = "项目合同", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(ProjectContract projectContract) {
        List<ProjectContract> list = projectContractService.selectProjectContractList(projectContract);
        ExcelUtil<ProjectContract> util = new ExcelUtil<ProjectContract>(ProjectContract.class);
        return util.exportExcel(list, "contract");
    }

    /**
     * 新增项目合同
     */
    @GetMapping("/add/{pId}")
    public String add(@PathVariable("pId") Long pId,ModelMap map) {
        map.put("pId",pId);
        return prefix + "/add";
    }

    /**
     * 新增保存项目合同
     */
    @RequiresPermissions("ruoyi:contract:add")
    @Log(title = "项目合同", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(ProjectContract projectContract) {
        //添加时判断
        //未支付金额为null的时候未支付金额则等于总金额;
        if (projectContract.getpNotpaid()==null){
            projectContract.setpNotpaid(projectContract.getpMoney());
        }
        //已支付金额为null的时候已支付金额为0.00;
        if(projectContract.getpPaid()==null){
            BigDecimal num= new BigDecimal("0.00");
            projectContract.setpPaid(num);
        }
        return toAjax(projectContractService.insertProjectContract(projectContract));
    }

    /**
     * 修改项目合同
     */
    @GetMapping("/edit/{pId}")
    public String edit(@PathVariable("pId") Long pId, ModelMap mmap) {
        ProjectContract projectContract = projectContractService.selectProjectContractById(pId);
        mmap.put("projectContract", projectContract);
        return prefix + "/edit";
    }

    /**
     * 修改保存项目合同
     */
    @RequiresPermissions("ruoyi:contract:edit")
    @Log(title = "项目合同", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(ProjectContract projectContract) {
        return toAjax(projectContractService.updateProjectContract(projectContract));
    }

    /**
     * 删除项目合同
     */
    @RequiresPermissions("ruoyi:contract:remove")
    @Log(title = "项目合同", businessType = BusinessType.DELETE)
    @PostMapping("/remove")
    @ResponseBody
    public AjaxResult remove(String ids) {
        return toAjax(projectContractService.deleteProjectContractByIds(ids));
    }
}
